from flask_script import Manager
from flask_migrate import MigrateCommand
from info import create_app

import datetime
import random




# 创建应用


app = create_app("dev")

# 创建管理器
mgr = Manager(app)

# 生成迁移命令保存短信验证码
mgr.add_command("mc", MigrateCommand)



@mgr.option('-u',dest='username')
@mgr.option('-p',dest='password')
def create_superuser(username, password):
    if not all([username,password]):
        app.logger.error('参数不足')
        return

    from info.models import User
    from info import db
    user = User()
    user.mobile = username
    user.password = password
    user.nick_name = username
    user.is_admin = True

    db.session.add(user)

    try:
        db.session.commit()
    except BaseException as e:
        db.session.rollback()
        app.logger.info('管理员和创建成功')


@mgr.command
def add_test_users():
    from info import db
    from info.models import User
    users = []
    now = datetime.datetime.now()
    for num in range(0, 10000):
        try:
            user = User()
            user.nick_name = "%011d" % num
            user.mobile = "%011d" % num
            user.password_hash = "pbkdf2:sha256:50000$SgZPAbEj$a253b9220b7a916e03bf27119d401c48ff4a1c81d7e00644e0aaf6f3a8c55829"
            user.create_time = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            users.append(user)
            print(user.mobile)
        except Exception as e:
            print(e)
    db.session.add_all(users)
    db.session.commit()
    print('OK')




if __name__ == '__main__':
    mgr.run()